package commandAction;

import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.util.Iterator;

import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import abbot.finder.BasicFinder;
import abbot.finder.ComponentNotFoundException;
import abbot.finder.MultipleComponentsFoundException;
import abbot.finder.matchers.WindowMatcher;
import abbot.tester.JListTester;

import com.supermap.desktop.Application;

import command.Command;

public class ActionDialog extends Action {
	@Override
	public Object action(Command cmd) {
		// TODO Auto-generated method stub
		// 默认在focused对话框上找空间点击，后面找指定名称对话框时用其他参数把名称传进来

		// 待完善：这个方法需要写成一定时间内循环寻找
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		JDialog dialog = null;
		String title = cmd.getTitle();
		if(title!=null){
			try {
				dialog = (JDialog) new BasicFinder().find(new WindowMatcher(title));
			} catch (ComponentNotFoundException | MultipleComponentsFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else{
			dialog = (JDialog) KeyboardFocusManager
					.getCurrentKeyboardFocusManager().getFocusedWindow();
		}
		Component c = ActionUtil.searchComponentByMap(dialog,
				cmd.getSearchMap());
		if (c == null || dialog == null || dialog.equals(c)) {
			Application.getActiveApplication().getOutput().output(" 控件未找到。");
			return null;
		}
		/*list 操作*/
		if (c instanceof JList && cmd.getValues() != null
				&& cmd.getValues().length > 0) {
			ActionUtil.selectListObject((JList) c, cmd.getValues());
			
		} else if (cmd.getAction() == null || cmd.getAction().length == 0) {
			robot.click(c);
		}
		return null;
	}
}
